package com.happy.Animation.controllers;

import com.happy.Animation.dao.CountryRepository;
import com.happy.Animation.entities.Country;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("countries")
public class CountryController {
    @Autowired
    CountryRepository countryRepository;

    @GetMapping
    public String displayCountries(Model model){
        List<Country> countries = countryRepository.findAll();
        model.addAttribute("countries",countries);
        return "countries/list-countries";
    }

    @GetMapping("/new")
    public String displayCountryForm(Model model){

        Country aCountry = new Country();
        model.addAttribute("country",aCountry);
        return "countries/new-country";
    }

    @PostMapping("/save")
    public String createCountry(Model model,Country country){
        countryRepository.save(country);

        return "redirect:/countries/new";
    }
}
